package com.zrrd._01_servlet.servlet;

import com.zrrd._01_servlet.mapper.UserMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author WanJl
 * @version 1.0
 * @title RequestDemo03
 * @description
 * @create 2024/1/13 10:37
 */
@WebServlet("/RequestDemo08")
public class RequestDemo08 extends HttpServlet {
    UserMapper userMapper=new UserMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决Post请求的中文乱码问题
        //设置字符输入流的编码，设置的字符集要和前端页面保持一致
        req.setCharacterEncoding("UTF-8");

        //获取post请求的请求体,根据前端传递过来的请求体中的属性名获取单个属性值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        boolean result=false;
        try {
                result= userMapper.login(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //如果登录成功，跳转到主页....
        if (result){
            resp.sendRedirect("/JMS2301_WEB_war/main.html");
        }else{
            resp.sendRedirect("/JMS2301_WEB_war/login.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
